import os
import yaml
import glob

last_pcd_file = None
file_name = None

# 获取当前用户的主目录
home_directory = os.path.expanduser("~")
config_path = os.path.join(home_directory, "Pcd2Las/src/pcd2las_python/config")

try:
    os.chdir(config_path)
    with open("file_msgs.yaml", "r") as f:
        file_msgs = yaml.load(f, Loader=yaml.FullLoader)
        # 拼接home_directory和配置文件中的路径
        input_folder = os.path.join(home_directory, file_msgs["input_folder"])
        output_folder = os.path.join(home_directory, file_msgs["output_folder"])
except (FileNotFoundError, KeyError) as e:
    print(f"加载配置文件时出现错误: {e}")
    input_folder = None
    output_folder = None

# 读取文件夹中最新的pcd文件的名称
def get_latest_pcd_file(directory):
    # 查找文件夹中的所有 .pcd 文件
    pcd_files = glob.glob(os.path.join(directory, "*.pcd"))
    if not pcd_files:
        return None  # 如果文件夹中没有 .pcd 文件，返回 None
    # 获取最后修改时间最新的文件
    latest_pcd_file= max(pcd_files, key=os.path.getmtime)
    # 获取最新文件的名称，去掉后缀.pcd
    global file_name 
    file_name = os.path.basename(latest_pcd_file).split(".")[0]
    return latest_pcd_file



if __name__ == '__main__':
   print(config_path)
   print(home_directory)
   print(input_folder)
   print(output_folder)
    